package com.syni.mdd.sjb.common.datatreaure.listen.appNews;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppUserNewsSwitch;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.service.DankMerchantsDefBusinessService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserNewsSwitchService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Component
@Log4j2
public class DataBankNewsSwitchListener implements ApplicationListener<AppNewsSwitchEvent> {

    @Autowired
    DxshAppUserNewsSwitchService dxshAppUserNewsSwitchService;
    @Autowired
    DankMerchantsDefBusinessService dankMerchantsDefBusinessService;

    @Override
    @Async
    public void onApplicationEvent(AppNewsSwitchEvent appNewsSwitchEvent) {
        log.info("======进入开关初始化======");

        Integer userId = appNewsSwitchEvent.getUserId();
//        Integer chooseBusinessId = dankMerchantsDefBusinessService.getChooseBusinessId(userId);
//        if(chooseBusinessId != null){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserNewsSwitch.USER_ID,userId);
//            queryWrapper.eq(DxshAppUserNewsSwitch.BUSINESS_ID,chooseBusinessId);
        List<DxshAppUserNewsSwitch> dxshAppUserNewsSwitches = dxshAppUserNewsSwitchService.list(queryWrapper);
        if(!CollectionUtils.isNotEmpty(dxshAppUserNewsSwitches)){
            List<DxshAppUserNewsSwitch> dxshAppUserNewsMessageTypes = new ArrayList<>();
            //首先三大订单类型
            Integer[] switchs = new Integer[]{Constants.DANK_ORDER_STATUS,Constants.DANK_MARKET_REALITY,Constants.DANK_OTHER_INFORM};
            for(Integer messageType : switchs) {
                DxshAppUserNewsSwitch dxshAppUserNewsSwitch = new DxshAppUserNewsSwitch();
                dxshAppUserNewsSwitch.setIsOpen(Constants.COMMON_FLAG_TRUE_INT);
                dxshAppUserNewsSwitch.setNewTime(LocalDateTime.now());
                dxshAppUserNewsSwitch.setMessageType(messageType);
                dxshAppUserNewsSwitch.setUserId(userId);
//                    dxshAppUserNewsSwitch.setBusinessId(chooseBusinessId);
                dxshAppUserNewsMessageTypes.add(dxshAppUserNewsSwitch);
            }
            if(dxshAppUserNewsMessageTypes.size() > 0){
                dxshAppUserNewsSwitchService.saveBatch(dxshAppUserNewsMessageTypes);
            }
            List<DxshAppUserNewsSwitch> childTypesList = new LinkedList<>();
            if(dxshAppUserNewsMessageTypes.size() > 0){
                for(DxshAppUserNewsSwitch dxshMessageType : dxshAppUserNewsMessageTypes){
                    if(dxshMessageType.getMessageType().equals(Constants.DANK_ORDER_STATUS)){
                        Integer[] childTypes = new Integer[]{Constants.DANK_ORDER_STATUS_MINAPP,Constants.DANK_ORDER_STATUS_MDD};
                        for(Integer childType : childTypes) {
                            DxshAppUserNewsSwitch dxshAppUserNewsSwitch = new DxshAppUserNewsSwitch();
                            dxshAppUserNewsSwitch.setIsOpen(Constants.COMMON_FLAG_TRUE_INT);
                            dxshAppUserNewsSwitch.setNewTime(LocalDateTime.now());
                            dxshAppUserNewsSwitch.setSecondMessageType(childType);
                            dxshAppUserNewsSwitch.setFirstMessageTypeId(dxshMessageType.getId());
                            dxshAppUserNewsSwitch.setUserId(userId);
//                                dxshAppUserNewsSwitch.setBusinessId(chooseBusinessId);
                            childTypesList.add(dxshAppUserNewsSwitch);
                        }
                    }else if(dxshMessageType.getMessageType().equals(Constants.DANK_OTHER_INFORM)){
                        Integer[] childTypes = new Integer[]{Constants.DANK_OTHER_INFORM_MINAPP,Constants.DANK_OTHER_INFORM_MDD
                                ,Constants.DANK_OTHER_INFORM_CUSTOMER_NEWS,Constants.DANK_OTHER_INFORM_COM_NEW,Constants.DANK_OTHER_INFORM_SYS_NOTIFICATION};
                        for(Integer childType : childTypes) {
                            DxshAppUserNewsSwitch dxshAppUserNewsSwitch = new DxshAppUserNewsSwitch();
                            dxshAppUserNewsSwitch.setIsOpen(Constants.COMMON_FLAG_TRUE_INT);
                            dxshAppUserNewsSwitch.setNewTime(LocalDateTime.now());
                            dxshAppUserNewsSwitch.setSecondMessageType(childType);
                            dxshAppUserNewsSwitch.setFirstMessageTypeId(dxshMessageType.getId());
                            dxshAppUserNewsSwitch.setUserId(userId);
//                                dxshAppUserNewsSwitch.setBusinessId(chooseBusinessId);
                            childTypesList.add(dxshAppUserNewsSwitch);
                        }
                    }
                }
            }
            if(childTypesList.size() > 0){
                dxshAppUserNewsSwitchService.saveBatch(childTypesList);
            }
        }
//        }
    }
}
